package com.zjht.gmcc.bean.action;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;

public class PublishUtil {

	public static String[] PublishHeaderQ = new String[13];

	public static ArrayList PublishHeadersM = new ArrayList();
	public static ArrayList PublishHeadersP = new ArrayList();

	// 清空首页公告缓存，以便重新从数据库中取首页公告信息
	public static void ResetPublish() {
		PublishUtil.ResetPublish("M");
		PublishUtil.ResetPublish("P");
		PublishUtil.ResetPublish("Q");
		try {
			for (int i = 0; i < 22; i++) {
				GetPublishHeader("M", i);
				GetPublishHeader("P", i);
			}
			GetPublishHeader("Q", 0);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// JDBCUtil.CloseConnection(conn);
		}
	}

	// 清空首页公告缓存，以便重新从数据库中取首页公告信息
	private static void ResetPublish(String strActorCode) {
		if ("M".equals(strActorCode)) {
			PublishHeadersM.clear();
		} else if ("P".equals(strActorCode)) {
			PublishHeadersP.clear();
		} else if ("Q".equals(strActorCode)) {
			for (int i = 0; i < PublishHeaderQ.length; i++) {
				PublishHeaderQ[i] = null;
			}
		}
	}

	// 获取首页公告列表(intAreaID:省级为0)
	private static void GetPublishHeader(String strActorCode, int intAreaID) {
		if (!"M".equals(strActorCode) && !"P".equals(strActorCode) && !"Q".equals(strActorCode))
			return;
		if (intAreaID < 0 || intAreaID > 21)
			return;
		boolean isArea = "M".equals(strActorCode) || "P".equals(strActorCode);// 需要区分地市
		String[] PublishHeader = new String[13];

		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			String strSQL = "SELECT * FROM (SELECT publishboardid,title,pcontent FROM PUBLISHBOARD WHERE pright LIKE ? "
					+ (isArea ? " AND (AreaID=-1 OR AreaID=?)" : "") + " ORDER BY publishboardid DESC) a WHERE ROWNUM=1";

			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ps.setString(1, "%" + strActorCode + "%");
			if (isArea)
				ps.setInt(2, intAreaID);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				PublishHeader[0] = rs.getString("publishboardid");
				String strTitle = rs.getString("title");

				String strContent = rs.getString("pcontent");

				if (strTitle.length() > 25)
					strTitle = strTitle.substring(0, 25) + "…";

				if (strContent.length() > 220) {
					strContent = strContent.substring(0, 220) + "……";
				}
				PublishHeader[1] = MyFormat.removeAllHTML(strTitle);
				PublishHeader[2] = MyFormat.removeAllHTML(strContent);
			}

			if (PublishHeader[0] != null && PublishHeader[1] != null && PublishHeader[2] != null) {
				strSQL = "SELECT publishboardid,title FROM "
						+ "(SELECT publishboardid,title FROM PUBLISHBOARD WHERE pright LIKE ? AND publishboardid<>?"
						+ (isArea ? " AND (AreaID=-1 OR AreaID=?)" : "") + " ORDER BY publishboardid DESC) a WHERE ROWNUM<=5";
				ps = myJDBC.prepareStatement(strSQL);
				ps.setString(1, "%" + strActorCode + "%");
				ps.setString(2, PublishHeader[0]);
				if (isArea)
					ps.setInt(3, intAreaID);
				rs = ps.executeQuery();
				int i = 2;
				while (rs.next()) {
					i++;
					PublishHeader[i] = rs.getString("publishboardid");
					if (PublishHeader[i] == null || PublishHeader[i].length() == 0)
						break;

					String strTitle = rs.getString("title");
					i++;
					if (strTitle.length() > 50)
						strTitle = strTitle.substring(0, 50) + "…";
					PublishHeader[i] = strTitle;
					if (PublishHeader[i] == null || PublishHeader[i].length() == 0)
						break;
				}
				if ("M".equals(strActorCode)) {
					PublishHeadersM.add(PublishHeader);
				} else if ("P".equals(strActorCode)) {
					PublishHeadersP.add(PublishHeader);
				} else if ("Q".equals(strActorCode)) {
					PublishHeaderQ = PublishHeader;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
	}
}
